//index.js
//获取应用实例
const app = getApp()
// const request = require('../../utils/request')
// const request = require("../../utils/request-mock")
const requests = require("../../utils/request")
const waitFlag = require("../../utils/waitFlag")
const buryDataStep = require('../../utils/buryDataStep.js');
const config = require('../../utils/config.js');

Page({
    data: {
        userInfo: {},
        hasUserInfo: false,
        hasUserSportInfo: false,
        myOpenId: "",
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        //弹窗是否显示设置
        isShowDialogModal: false,
        isShowGetStep: false,
        isShowGetRedbag: false,
        canGetRedbag: false,
        onlyNeedSport: false,
        isShowRedbagDetail: false,
        isShowNoneNetwork: false,
        isShowNewUserRedbagmodal: false,
        isShowInviteSuccessmodal: false,
        isShowOtherMoreRedbagmodal:false,
        //  新增步数
        newAddStep: 0,
        //  总步数
        myTotalStep: 0,
        // 是否是新用户
        isNewUser: false,
        // 获取步数弹窗的提示文字
        GetStepText: "",
        // 获取到的新增步数
        getAddStep: 0,
        session_key: "",
        // 剩余金钱
        money: 0,
        // 开红包获得的钱
        add_money: 0,
        // 用户名
        nick_name: "",
        // 用户头像
        avatar_url: "",
        // 汇率
        exchange_rate: 10000,
        // 可以换取的最小步数
        min_openRedbag_step: 3000,
        // 是否需要加回初始请求权限弹窗
        is_show_DialogModal_once: false,
        // 红包按钮文字
        RedbagDetailWord: "获取更多步数",
        // 按钮泡泡
        reward_step_list: [],
        // 按钮泡泡左
        reward_step_list_left: [],
        // 按钮泡泡右
        reward_step_list_right: [],
        // 冒泡字的配置
        reward_word_1: "广告",
        reward_word_2: "赚步数",
        reward_word_3: "邀请好友",
        reward_word_4: "今日新增",
        reward_word_5: "好友加成",
        // 最多显示冒泡数
        max_bubble: 5,
        // 好友列表
        friends_list: [],
        // 新用户第一次打开
        new_user_open_redbag:false,
        // 是否是第一次进入
        is_first_enter :true,
        // 是否重新拉起权限访问接口
        isShowTestModal:false,
        leftPx:150.5,
        GetStepad_Style:"",
        GetMoreStep_ad_text_left:"",
        RedbagDetailAd:"",
        //是否是打开第二个包
        is_open_second_bag:false,
        // 开始看红包广告
        begin_look_redbag_Ad:false,
        // 红包是否看够了时间
        is_enough_open_second_redbag:false,
        foot_share_click:false,
        // 最短时间
        min_look_time:5,
        min_open_time:5,
        is_look_adv:false,
        hide_time:0,
        show_catad:true,
        switch_reward:false,
        // 可能加倍的rewardId
        maybe_double_id:"",
        GetStepad_top_str:"",
        // 广告是否加载成功
        index_ad_load_success:true,
        index_redbagAd_success:true,
        foot_ad_success:true,
        // 关闭弹窗是否加步数
        is_close_step_to_add:true,
        today_exchange_times:0,
    },

    onLoad: function () {
        let that = this
        that.setData({
            // isShowDialogModal:false,
            newAddStep: 0,
            myTotalStep: 0,
        })

        // 分割线
        waitFlag.waitFlag(app.globalData, 'hasGet', false).then(() => {
            if (app.globalData.statusBarHeight>20){
                let add_height = 205 + (app.globalData.statusBarHeight - 20)*375/app.globalData.windowWidth
                let style_text = add_height + "rpx"
                that.setData({
                    GetStepad_Style:style_text
                })
            }else {
                that.setData({
                    GetStepad_Style:"220rpx"
                })
            }

            if (app.globalData.is_xs_max){
                that.setData({
                    GetStepad_top_str:"left: -150px !important;"
                })
            }else {
                if (app.globalData.is_plus_phone){
                    that.setData({
                        GetStepad_top_str:"left: -269rpx",
                        RedbagDetailAd:"left:56px",
                        GetMoreStep_ad_text_left:"left:-40rpx"
                    })
                }
            }

            let isCloseMiniProgramGuide = wx.getStorageSync('isCloseMiniProgramGuide');
            if (wx.getLaunchOptionsSync().scene==1089&&isCloseMiniProgramGuide){
                requests.getRewardStep(app.globalData.this_app_id,false).then(returnData=>{
                    if (returnData.reward_step_list) {
                        that.handle_reward_step_list_data(that,returnData.reward_step_list)
                    }
                })
            }

            that.setData({
                is_first_enter:false,
                switch_reward:config.banner_status,
                today_exchange_times:app.globalData.initUserInfo.data.today_exchange_times,
                // switch_reward:true
            })
            console.log("开关状态")
            console.log(config.banner_status)
            console.log("开关状态")
            if (!app.globalData.initUserInfo.is_new) {
                if (!app.globalData.userInfo) {
                    wx.getSetting({
                        success(res) {
                            if (res.authSetting["scope.userInfo"]) {
                                wx.getUserInfo({
                                    success(res) {
                                        app.globalData.userInfo = res.userInfo
                                    }
                                })
                                that.setData({
                                    // isShowDialogModal:false,
                                    hasUserInfo: true,
                                    isNewUser: app.globalData.initUserInfo.is_new,
                                    // newAddStep:app.globalData.initUserInfo.data.add_step,
                                    myTotalStep: app.globalData.initUserInfo.data.all_step,
                                    myOpenId: app.globalData.initUserInfo.open_id,
                                    userInfo: app.globalData.userInfo
                                })
                                that.handle_reward_step_list_data(that, app.globalData.initUserInfo.data.reward_step_list)
                                that.handle_friend_list(that, app.globalData.initUserInfo.data.friends)
                                that.getSportInfo(that)
                            } else {
                                that.setData({
                                    isShowDialogModal: true,
                                    hasUserInfo: false
                                })
                            }
                        }
                    })
                }else {
                    that.setData({
                        hasUserInfo: true,
                        isNewUser: app.globalData.initUserInfo.is_new,
                        myTotalStep: app.globalData.initUserInfo.data.all_step,
                        myOpenId: app.globalData.initUserInfo.open_id,
                        userInfo: app.globalData.userInfo
                    })
                    that.handle_reward_step_list_data(that, app.globalData.initUserInfo.data.reward_step_list)
                    that.handle_friend_list(that, app.globalData.initUserInfo.data.friends)
                    that.getSportInfo(that)
                }
            } else {
                that.setData({
                    isShowNewUserRedbagmodal: true,
                    isShowDialogModal: false
                })
            }

        })

        wx.showShareMenu({
            withShareTicket: true,
            success: function (res) {
                // 分享成功
                console.log(res)
            },
            fail: function (res) {
                // 分享失败
                console.log(res)
            }
        })
    },

    onHide:function(){
        console.log("隐藏了")
        if (this.data.is_look_adv){
            this.setData({
                hide_time:Date.now()
            })
            console.log("查看此时的hide time")
            console.log(this.data.hide_time)
            console.log("查看此时的hide time")
        }
    },

    onShow:function(){
        let that = this
        this.is_reward_double(that)

        this.is_enough_open_second(that)

        this.is_enough_open_redbag_reward(that)

        this.share_open_redbag(that)

        this.read_cache_friendData(that)
        // requests.dataBuried(buryDataStep.home_show,'home_show',app.globalData.openId)
        if (!this.data.is_first_enter){
            wx.getSetting({
                success(res) {
                    if (!res.authSetting["scope.userInfo"]){
                        that.setData({
                            isShowDialogModal: true,
                            hasUserInfo: false
                        })
                    } else {
                        that.setData({
                            isShowDialogModal:false
                        })
                        wx.getWeRunData({
                            success(res) {
                                let encryptedData = res.encryptedData
                                let iv = res.iv
                                let nickName = that.data.userInfo.nickName
                                let avatarUrl = that.data.userInfo.avatarUrl
                                requests.refreshData(encryptedData, iv, nickName, avatarUrl).then(data => {
                                    let reward_step_list = data.data.data.reward_step_list
                                    that.handle_reward_step_list_data(that, reward_step_list)
                                    that.handle_friend_list(that, data.data.data.friends)
                                    that.setData({
                                        myTotalStep: data.data.data.all_step
                                    })
                                })
                            }, fail(res) {
                                // 查看授权失败原因
                                that.onshowWhyWerunFail(that)
                            }
                        })
                    }
                }
            })

        }
    },

    onShareAppMessage: function (res) {
        // 拼接分享url
        let url = "/pages/index/index?from_open_id=" + this.data.myOpenId
        let title = app.globalData.userInfo.nickName + "@你，【运动步数能赚钱】你还不知道吗？"
        console.log("分享出去的url")
        console.log(url)
        console.log("分享出去的url")
        return {
            title: title,
            imageUrl: "/images/publicize_3.jpg",
            path: url,
        }
    },

    getUserInfo: function (e) {
        this.setData({isShowDialogModal: false})
        // 未授权
        if (e.detail.userInfo) {
            app.globalData.userInfo = e.detail.userInfo
            this.setData({
                userInfo: e.detail.userInfo,
                hasUserInfo: true,
                // isShowDialogModal:true
            })
            if (!this.data.hasUserSportInfo) {
                // let that = this
                let isNewUser = app.globalData.initUserInfo.is_new
                if (app.globalData.initUserInfo) {
                    this.setData({
                        isNewUser: isNewUser,
                        // newAddStep:app.globalData.initUserInfo.data.add_step,
                        userInfo:app.globalData.userInfo,
                        myTotalStep: app.globalData.initUserInfo.data.all_step,
                        myOpenId: app.globalData.initUserInfo.open_id,
                    })
                }
                // 获取微信运动权限
                this.getSportInfo(this)
            } else {
                let isNewUser = app.globalData.initUserInfo.is_new
                if (app.globalData.initUserInfo) {
                    this.setData({
                        isNewUser: isNewUser,
                        // newAddStep: app.globalData.initUserInfo.data.add_step,
                        myTotalStep: app.globalData.initUserInfo.data.all_step,
                        myOpenId: app.globalData.initUserInfo.open_id,
                        // session_key:getData.session_key
                    })
                }
                this.getSportInfo(this)
            }

        } else {
            this.setData({
                isShowDialogModal: true
            })
        }
    },

    onPullDownRefresh() {
        let that = this
        // 检测当前网络
        wx.getNetworkType({
            success(networkStatus) {
                if (networkStatus.networkType != 'none'){
                    that.setData({
                        isShowNoneNetwork:false
                    })
                    // 执行后续逻辑
                    wx.stopPullDownRefresh({
                        success(res) {
                            // 获取最新的步数信息
                            wx.getWeRunData({
                                success(res) {
                                    let encryptedData = res.encryptedData
                                    let iv = res.iv
                                    let nickName = that.data.userInfo.nickName
                                    let avatarUrl = that.data.userInfo.avatarUrl
                                    requests.refreshData(encryptedData, iv, nickName, avatarUrl).then(data => {
                                        console.log("下拉刷新")
                                        console.log(data)
                                        let reward_step_list = data.data.data.reward_step_list
                                        that.handle_reward_step_list_data(that, reward_step_list)
                                        that.handle_friend_list(that, data.data.data.friends)
                                        that.setData({
                                            myTotalStep: data.data.data.all_step
                                        })
                                        wx.showToast({
                                            title: "步数刷新成功",
                                            icon: "none",
                                            duration: 1500
                                        })
                                    })
                                }, fail(res) {
                                    wx.showToast({
                                        title: "步数刷新失败",
                                        icon: "none",
                                        duration: 1500
                                    })
                                }
                            })
                        }
                    })
                }else {
                    that.setData({
                        isShowNoneNetwork:true
                    })
                }
            }
        })
    },

    getSportInfo(that) {
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.werun']) {
                    that.setData({isShowDialogModal: false})
                    wx.authorize({
                        scope: 'scope.werun',
                        success() {
                            wx.getWeRunData({
                                success(res) {
                                    // 步数字符串
                                    const encryptedData = res.encryptedData
                                    const iv = res.iv
                                    that.setData({
                                        isShowDialogModal: false,
                                        hasUserSportInfo: true
                                    })
                                    let nickName = that.data.userInfo.nickName
                                    let avatarUrl = that.data.userInfo.avatarUrl
                                    //获取到的步数
                                    requests.refreshData(encryptedData, iv, nickName, avatarUrl).then(data => {
                                        let all_step = data.data.data.all_step
                                        let reward_step_list = data.data.data.reward_step_list
                                        let money = data.data.data.money
                                        // 初始页用户数据
                                        that.handle_friend_list(that,data.data.data.friends)
                                        that.initUserStep(that, all_step, reward_step_list)
                                    })
                                },
                                fail(res) {
                                    console.log("请求运动权限失败" + res)
                                    return
                                }
                            })
                        }, fail(res) {
                            console.log("index请求步数权限失败" + res)
                            that.setData({
                                isShowTestModal:true
                            })
                            // that.setData({
                            //   isShowDialogModal:true,
                            //   onlyNeedSport:true,
                            //   hasUserInfo:true
                            // })
                        }
                    })
                } else {
                    that.setData({isShowDialogModal: false})
                    wx.getWeRunData({
                        success(res) {
                            // 步数字符串
                            const encryptedData = res.encryptedData
                            const iv = res.iv
                            that.setData({
                                hasUserSportInfo: true,
                                isShowDialogModal: false,
                                userInfo: app.globalData.userInfo
                            })
                            //获取到的步数(后续需要添加)
                            let nickName = that.data.userInfo.nickName
                            let avatarUrl = that.data.userInfo.avatarUrl
                            requests.refreshData(encryptedData, iv, nickName, avatarUrl).then(data => {
                                let all_step = data.data.data.all_step
                                let reward_step_list = data.data.data.reward_step_list
                                // 初始页用户数据
                                that.initUserStep(that, all_step, reward_step_list)
                            })
                        },
                        fail(res) {
                            return
                        }
                    })
                }

            }, fail() {
                console.log("失败")
            }
        })
    },

    initUserStep(that, all_step, reward_step_list) {
        that.handle_reward_step_list_data(that, reward_step_list)
        that.setData({
            myTotalStep: all_step
        })
    },

    newStep: function (e) {
        this.setData({
            isShowGetStep: true,
            maybe_double_id:e.currentTarget.id
        })

        // 处理步数函数
        this.show_get_step_modal(e.currentTarget.id,this)
        // let that = this
        requests.dataBuried(buryDataStep.home_new_click,'home_new_click',app.globalData.openId)
    },

    // 步数换钱页面
    openRedbag: function (e) {
        if (this.data.myTotalStep >= this.data.min_openRedbag_step) {
            this.setData({
                canGetRedbag: true,
                isShowGetRedbag: true
            })
        } else {
            this.setData({
                canGetRedbag: false,
                isShowGetRedbag: true
            })
        }
        // 埋点
        requests.dataBuried(buryDataStep.home_pack_show,'home_pack_show',app.globalData.openId)
    },

    // 红包详情
    redbagDetail: function (e) {
        console.log("可以领红包")
        let that = this
        // 如果已经过审
        if (this.data.switch_reward){
            if (this.data.today_exchange_times>0){
                console.log("弹出弹窗")
                that.setData({
                    is_open_second_bag:true,
                    is_look_adv:true,
                    isShowOtherMoreRedbagmodal:true,
                    isShowGetRedbag:false
                })
                // that.open_red_bag(that)
            } else {
                console.log("今天首次打开红包")
                that.open_red_bag(that)
            }
        }else {
            that.open_red_bag(that)
        }
    },

    click_ad: function (e) {
        console.log("点广告了")
    },

    // 邀请好友
    InvateFriend: function (e) {
        console.log("点击了邀请好友")
        let that = this
        requests.invate(e).then(data => {
            if (data.isSuccess) {
                let newAddStep = that.data.newAddStep + 5000
                that.setData({
                    newAddStep: newAddStep,
                    GetStepText: "获得运动步数",
                    getAddStep: newAddStep,
                    isShowGetStep: false
                })
            }
        })
    },

    // 获取更多步数
    RedbagDetailGetMoreStep: function (e) {
        // wx.switchTab({
        //   url:"/pages/getSteps/getSteps"
        // })
        this.setData({
            isShowRedbagDetail: false,
            new_user_open_redbag:false
        })
        requests.dataBuried(buryDataStep.home_pack_click,'home_pack_click',app.globalData.openId)
    },

    // 关闭红包领取弹窗按钮
    closeShowGetRedbag: function (e) {
        this.setData({
            isShowGetRedbag: false
        })
    },

    // 关闭新增步数弹窗
    closeGetStepModal: function (e) {
        this.setData({
            isShowGetStep: false,
            is_look_adv:false
        })
        let that = this
        if (this.data.is_close_step_to_add){
            requests.NewAddSteps(this.data.maybe_double_id,false).then(getData => {
                console.log("++++++++++")
                let getAddStep = this.data.newAddStep
                that.setData({
                    GetStepText: "获得运动步数",
                    myTotalStep: getData.data.all_step,
                    getAddStep: getData.data.add_step,
                    is_look_adv:false,
                    show_catad:true
                })
                that.handle_reward_step_list_data(that,getData.data.reward_step_list)
            })
        }
        this.setData({
            is_close_step_to_add:true
        })
    },

    // 关闭红包具体页面
    closeRedbagDetail: function (e) {
        this.setData({
            isShowRedbagDetail: false,
            is_look_adv:false,
            begin_look_redbag_Ad:false,
            new_user_open_redbag:false
        })
    },

    // 关闭红包无法领取页面
    closeNotEnoughModal: function (e) {
        this.setData({
            isShowGetRedbag: false
        })
    },

    hide_modal: function (e) {
        this.setData({isShowDialogModal: false})
    },

    // 关闭新手领取红包按钮
    closeNewUserRedbag() {
        this.setData({
            isShowNewUserRedbagmodal: false,
            isShowDialogModal: true
        })
    },

    // 新手领红包
    newUserRedbagDetail() {
        this.setData({
            isShowRedbagDetail: true,
            add_money: 0.15,
            isShowNewUserRedbagmodal: false,
            isShowDialogModal: true,
            RedbagDetailWord: "获取更多红包",
            new_user_open_redbag:true
        })
    },

    // 今日首次领取红包
    open_red_bag(that){
        if (that.data.switch_reward) {
            if (that.data.today_exchange_times>0){
                if (that.data.is_enough_open_second_redbag){
                    if (that.data.index_redbagAd_success){
                        that.setData({
                            is_look_adv:true,
                            begin_look_redbag_Ad:true
                        })
                    }else {
                        that.setData({
                            is_look_adv:false,
                            begin_look_redbag_Ad:false
                        })
                    }
                }
            }else {
                requests.exchange_money().then(getData => {
                    if (parseInt(getData.data.code) == 0) {
                        that.setData({
                            money: getData.data.money,
                            add_money: getData.data.add_money / that.data.exchange_rate,
                            myTotalStep: getData.data.all_step,
                            isShowGetRedbag: false,
                            isShowRedbagDetail: true,
                            RedbagDetailWord: "获取更多步数",
                            today_exchange_times:getData.data.today_exchange_times,
                            is_look_adv:false,
                            isShowOtherMoreRedbagmodal:false
                        })
                        if (that.data.index_redbagAd_success){
                            that.setData({
                                is_look_adv:true,
                                begin_look_redbag_Ad:true
                            })
                        }
                    } else {
                        console.log("这是个错误")
                        console.log(getData)
                        that.setData({
                            add_money: 0.02,
                            myTotalStep: 2139,
                            isShowGetRedbag: false,
                            isShowRedbagDetail: false
                        })
                    }
                })
            }
        }else {
            requests.exchange_money().then(getData => {
                if (parseInt(getData.data.code) == 0) {
                    that.setData({
                        money: getData.data.money,
                        add_money: getData.data.add_money / that.data.exchange_rate,
                        myTotalStep: getData.data.all_step,
                        isShowGetRedbag: false,
                        isShowRedbagDetail: true,
                        RedbagDetailWord: "获取更多步数",
                        today_exchange_times:getData.data.today_exchange_times,
                        is_look_adv:false,
                        isShowOtherMoreRedbagmodal:false
                    })
                    if (that.data.index_redbagAd_success){
                        that.setData({
                            is_look_adv:true,
                            begin_look_redbag_Ad:true
                        })
                    }
                } else {
                    console.log("这是个错误")
                    console.log(getData)
                    that.setData({
                        add_money: 0.02,
                        myTotalStep: 2139,
                        isShowGetRedbag: false,
                        isShowRedbagDetail: false
                    })
                }
            })
        }
    },

    // 处理reward_step_list数据
    handle_reward_step_list_data(that, reward_step_list) {
        let reward_step_list_left = []
        let reward_step_list_right = []
        that.setData({
            reward_step_list:reward_step_list
        })
        let index = 0
        let has_4 = false
        // 处理4的特殊情况
        if (reward_step_list[4]) {
            let new_json_data = {}
            new_json_data.type = 4
            new_json_data.value = reward_step_list[4]
            new_json_data.s_left = "25rpx"
            new_json_data.s_top = "0rpx"
            new_json_data.way = that.data.reward_word_4
            new_json_data.aniamtionStr = that.random_animation(that)
            reward_step_list_left.push(new_json_data)
            index += 1
        }
        for (var type in reward_step_list) {
            let cur_data = {}
            cur_data.type = type
            if (parseInt(type) == 4) {
                continue
            }
            cur_data.value = reward_step_list[type]
            // cur_data.s_left = parseInt(index % 2 + 1) * 25 + Math.random() * 50 + "rpx"
            // cur_data.s_top = parseInt(index / 2) * 120 + "rpx"
            // 随机动画效果
            cur_data.aniamtionStr = that.random_animation(that)
            if (parseInt(type) == 5) {
                // 添加获取步数的途径字
                cur_data.s_left = 50 + "rpx"
                cur_data.s_top = 120 + "rpx"
                cur_data.way = that.data.reward_word_5
                reward_step_list_right.push(cur_data)
            } else if (parseInt(type) == 2) {
                cur_data.s_left = 25 + "rpx"
                cur_data.s_top = 0
                cur_data.way = that.data.reward_word_2
                reward_step_list_right.push(cur_data)
            } else if (parseInt(type) == 3) {
                cur_data.s_left = 50 + "rpx"
                cur_data.s_top = 120 + "rpx"
                cur_data.way = that.data.reward_word_3
                reward_step_list_left.push(cur_data)
            } else if (parseInt(type) == 1) {
                cur_data.way = that.data.reward_word_1
                cur_data.s_left = 65 + "rpx"
                cur_data.s_top = 240 + "rpx"
                reward_step_list_right.push(cur_data)
            }

            // if (index % 2 == 0 && index != 5 - 1) {
            //     reward_step_list_left.push(cur_data)
            // } else {
            //     reward_step_list_right.push(cur_data)
            // }
            index += 1
        }

        that.setData({
            reward_step_list_left: reward_step_list_left,
            reward_step_list_right: reward_step_list_right
        })
    },

    // 随机动画方法
    random_animation(that) {
        let random_num = Math.random()
        if (random_num < 0.25) {
            return "animation: moves_1 1.5s linear infinite; animation-direction:alternate;"
        } else if (random_num < 0.5) {
            return "animation: moves_2 2s linear infinite; animation-direction:alternate;"
        } else if (random_num < 0.75) {
            return "animation: moves_3 1.8s linear infinite; animation-direction:alternate;"
        } else {
            return "animation: moves_4 1.8s linear infinite; animation-direction:alternate; animation-delay:0.1s;"
        }
    },

    closeShowInviteSuccess: function (e) {
        this.setData({
            isShowInviteSuccessmodal: false
        })
    },

    handle_friend_list(that, friend_list) {
        // 使用的friend_list
        let friend_list_length = friend_list.length
        // 要且只要十个数据
        let new_list = []
        if (friend_list_length == 10) {
            new_list = friend_list
        } else if (friend_list_length > 10) {
            for (let i = 0; i < 10; i++) {
                new_list.push(friend_list[i])
            }
        } else {
            for (let i = 0; i < friend_list_length; i++) {
                new_list.push(friend_list[i])
            }
            // 填充数据到10个
            for (let j = 0; j < 10 - friend_list_length; j++) {
                let fill_json = {}
                fill_json.avatar_url = "/images/icon-default_avatar.png"
                new_list.push(fill_json)
            }
        }
        for (let i = 0; i < 10; i++) {
            let cur_data = new_list[i]
            let t_left = (i * 60 + 37) + "rpx"
            cur_data.t_left = t_left
        }

        that.setData({
            friends_list: new_list
        })
        // 缓存测试
        that.cache_friendData(new_list)

        app.globalData.friend_list = friend_list
    },

    nav_to_friend_list: function (e) {
        wx.navigateTo({
            url: '../friend_list/friend_list'
        })
    },

    // 检测微信步数授权
    detect_werun:function(e){
        let that = this
        this.setData({
            isShowTestModal:false,
        })
        wx.openSetting({
            success(res) {
                if (res.authSetting["scope.werun"]&&res.authSetting["scope.userInfo"]){
                    that.setData({
                        hasUserSportInfo:true,
                        hasUserInfo:true
                    })
                    that.getSportInfo(that)
                }else {
                    that.setData({
                        isShowTestModal:true,
                    })
                }
            }
        })
    },

    // 是否足够时间打开第二个红包
    is_enough_open_second(that){
        if (that.data.hide_time){
            console.log("此时的时间差")
            console.log(Date.now()-that.data.hide_time)
            console.log("此时的时间差")
        }

         if (that.data.is_open_second_bag&&that.data.hide_time) {
             if (Date.now()-that.data.hide_time>that.data.min_open_time*1000) {
                 console.log("看够时间了，显示红包")
                 that.setData({
                     is_enough_open_second_redbag:true
                 })
                 requests.exchange_money().then(getData=>{
                     if (parseInt(getData.data.code) == 0) {
                         that.setData({
                             money: getData.data.money,
                             add_money: getData.data.add_money / that.data.exchange_rate,
                             myTotalStep: getData.data.all_step,
                             isShowGetRedbag: false,
                             isShowRedbagDetail: true,
                             RedbagDetailWord: "获取更多步数",
                             today_exchange_times:getData.data.today_exchange_times,
                             is_look_adv:false,
                             isShowOtherMoreRedbagmodal:false,
                             is_enough_open_second_redbag:false,
                             is_open_second_bag:false
                         })

                         that.setData({
                             is_look_adv:true,
                             begin_look_redbag_Ad:true
                         })

                     } else {
                         console.log("这是个错误")
                         console.log(getData)
                         that.setData({
                             add_money: 0.02,
                             myTotalStep: 2139,
                             isShowGetRedbag: false,
                             isShowRedbagDetail: false
                         })
                     }
                 })
                 // that.open_red_bag(that)
             }else {
                 console.log("不可以打开红包")
             }
         }
    },

    // 领取步数弹窗分享数据埋点
    share_getStep:function(e){
        console.log("获取步数数据埋点")
        requests.dataBuried(buryDataStep.home_new_share,'home_new_share',app.globalData.openId)
    },

    // 首页分享按钮数据埋点
    share_btn_index:function(e){
        requests.dataBuried(buryDataStep.home_share_click,'home_share_click',app.globalData.openId)
    },

    // 红包弹窗分享埋点
    pack_share:function(e){
        requests.dataBuried(buryDataStep.home_pack_click,'home_pack_click',app.globalData.openId)
    },

    // 缓存测试
    cache_friendData(friend_list){
        wx.setStorage({
            key: 'friend_list',
            data: friend_list})
    },

    // 读取好友列表缓存
    read_cache_friendData(that){
        wx.getStorage({
            key:"friend_list",
            success(res) {
                that.setData({
                    friends_list:res.data
                })
            }
        })
    },

    show_get_step_modal(currentTargetId,that){
        this.setData({
            GetStepText: "获得运动步数",
            getAddStep: that.data.reward_step_list[currentTargetId],
            is_look_adv:true,
            show_catad:true
        })
    },

    onshowWhyWerunFail(that){
        wx.getSetting({
            success(res) {
                // 未授权
                if (!res.authSetting["scope.werun"]){
                    that.setData({
                        isShowTestModal:true
                    })
                } else {
                    wx.getWeRunData({
                        success(res) {
                            let encryptedData = res.encryptedData
                            let iv = res.iv
                            let nickName = that.data.userInfo.nickName
                            let avatarUrl = that.data.userInfo.avatarUrl
                            requests.refreshData(encryptedData, iv, nickName, avatarUrl).then(data => {
                                let reward_step_list = data.data.data.reward_step_list
                                that.handle_reward_step_list_data(that, reward_step_list)
                                that.handle_friend_list(that, data.data.data.friends)
                                that.setData({
                                    myTotalStep: data.data.data.all_step
                                })
                            })
                        }
                    })
                }
            }
        })
    },

    // 步数奖励
    is_reward_double(that){
        if (this.data.is_look_adv&&this.data.hide_time){
            if (Date.now()-this.data.hide_time>that.data.min_look_time*1000){
                this.setData({
                    is_close_step_to_add:false
                })
                if (!this.data.begin_look_redbag_Ad){
                    this.add_step_to_user(this,true)
                }
            }else {
                console.log("木有")
            }
        }
    },

    add_step_to_user(that,is_double){
        requests.NewAddSteps(that.data.maybe_double_id,is_double).then(getData => {
            if (parseInt(getData.data.code) == 1) {
                this.setData({
                    GetStepText: "暂时没有步数可以领取",
                    getAddStep: 0,
                    is_look_adv:false,
                })
            } else {
                let getAddStep = that.data.newAddStep
                that.setData({
                    GetStepText: "获得运动步数",
                    myTotalStep: getData.data.all_step,
                    getAddStep: getData.data.add_step,
                    is_look_adv:false,
                    show_catad:false,
                })
                that.handle_reward_step_list_data(that,getData.data.reward_step_list)
            }
        })
    },

    is_enough_open_redbag_reward(that){
        if (that.data.hide_time&&that.data.begin_look_redbag_Ad) {
            if (that.data.begin_look_redbag_Ad&&Date.now()-that.data.hide_time>that.data.min_open_time*1000){
                //增加步数(接口调好后需要加入逻辑)
                requests.getAdRewardStep(1,false).then(returnData=>{
                    that.setData({
                        begin_look_redbag_Ad:false,
                        is_look_adv:false,
                        isShowRedbagDetail:false
                    })
                    if (returnData.reward_step_list) {
                        that.handle_reward_step_list_data(that,returnData.reward_step_list)
                    }
                })
            }else if (!that.data.begin_look_redbag_Ad) {
                that.setData({
                    begin_look_redbag_Ad:false,
                    is_look_adv:false,
                    isShowRedbagDetail:false
                })
            }else {
                console.log("没看够时间")
            }
        }
    },

    closeOtherMoreRedbagmodal:function(e){
        this.setData({
            isShowOtherMoreRedbagmodal:false,
            is_open_second_bag:false,
            is_look_adv:false,
        })
    },

    closeGetStepShow:function(e){
        this.setData({
            isShowOtherMoreRedbagmodal:false,
            is_look_adv:false,
            is_open_second_bag:false
        })
    },

    foot_share:function(e){
        this.setData({
            foot_share_click:true
        })
    },

    share_open_redbag(that){
        if (that.data.foot_share_click){
            requests.exchange_money().then(getData=>{
                if (parseInt(getData.data.code) == 0) {
                    that.setData({
                        money: getData.data.money,
                        add_money: getData.data.add_money / that.data.exchange_rate,
                        myTotalStep: getData.data.all_step,
                        isShowGetRedbag: false,
                        isShowRedbagDetail: true,
                        RedbagDetailWord: "获取更多步数",
                        today_exchange_times:getData.data.today_exchange_times,
                        is_look_adv:false,
                        isShowOtherMoreRedbagmodal:false,
                        is_enough_open_second_redbag:false,
                        is_open_second_bag:false,
                        foot_share_click:false
                    })

                    that.setData({
                        is_look_adv:true,
                        begin_look_redbag_Ad:true
                    })

                } else {
                    console.log("这是个错误")
                    console.log(getData)
                    that.setData({
                        add_money: 0.02,
                        myTotalStep: 2139,
                        isShowGetRedbag: false,
                        isShowRedbagDetail: false
                    })
                }
            })
        }
    },

    //广告成功加载
    index_ad_load:function(e){
        this.setData({
            index_ad_load_success:true
        })
    },

    // 广告加载错误
    index_ad_error:function(e){
        console.log("广告未加载成功")
        this.setData({
            index_ad_load_success:false
        })
    },

    index_redbagAd_error:function(e){
        this.setData({
            index_redbagAd_success:false,
        })
    },

    index_redbagAd_load:function(e){
        this.setData({
            index_redbagAd_success:true
        })
    },

    // 第二次红包广告
    foot_ad_error:function(e){
        this.setData({
            foot_ad_success:false
        })
    },

    foot_ad_load:function(e){
        this.setData({
            foot_ad_success:true
        })
    },

    // 测试使用 清除数据
    cleanData: function (e) {
        console.log("清除数据")
        requests.cleanData()
    }

})
